package com.efounder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.addressselector.BottomDialog;
import com.efounder.addressselector.OnAddressSelectedListener;
import com.efounder.addressselector.model.City;
import com.efounder.addressselector.model.County;
import com.efounder.addressselector.model.Province;
import com.efounder.addressselector.model.Street;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.efounder.mobilemall.R;
import com.efounder.util.AbStrUtil;
import com.efounder.util.ToastUtil;
import com.efounder.utils.MallRequestUtil;
import com.efounder.utils.ResStringUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import static com.efounder.utils.MallRequestUtil.ADDRESS_CREATE_URL;

/**
 * @deprecated
 * Created by will on 18-2-8.
 */

public class MallAddAddressActivity extends Activity implements View.OnClickListener{

    private String TAG = this.getClass().getSimpleName();

    private EditText etDistrict, etPhone, etName, etSpecific;
    private TextView tvSave;
    private CheckBox defaultAddressCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_add_address);
        initView();
    }

    public void initView() {
        View transparentView = (View) findViewById(R.id.transparent_view);
        transparentView.setOnClickListener(this);
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        etName = (EditText) findViewById(R.id.et_address_name);
        etPhone = (EditText) findViewById(R.id.et_address_phone);
        etDistrict = (EditText) findViewById(R.id.et_address_district);
        etSpecific = (EditText) findViewById(R.id.et_address_specific);
        tvSave = (TextView) findViewById(R.id.tv_save_address);
        defaultAddressCheck = (CheckBox) findViewById(R.id.cb_default_address);
        defaultAddressCheck.setChecked(false);
        defaultAddressCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            }
        });

        etDistrict.setInputType(InputType.TYPE_NULL);
        etDistrict.setOnClickListener(this);
        tvSave.setOnClickListener(this);
    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(R.anim.bottom_dialog_exit,0);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.et_address_district) {
            final BottomDialog dialog = new BottomDialog(MallAddAddressActivity.this);
            dialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                @Override
                public void onAddressSelected(Province province, City city, County county, Street street) {
                    etDistrict.setText(province.name + city.name + county.name);
                    dialog.dismiss();
                }
            });
            dialog.show();
        } else if (id == R.id.tv_save_address) {
            if (etPhone.getText().toString().equals("") || !AbStrUtil.isMobileNo(etPhone.getText().toString())){
                ToastUtil.showToast(this, ResStringUtil.getString(R.string.mall_address_enter_right_phone));
                return;
            }
            if (etName.getText().toString().equals("") || etSpecific.getText().toString().equals("")
                    || etDistrict.getText().toString().equals("")){
                ToastUtil.showToast(this, ResStringUtil.getString(R.string.mall_address_enter_right_info));
                return;
            }
            HashMap<String, String> map = new HashMap<>();
            map.put("userId", EnvironmentVariable.getUserName());
            map.put("userName", etName.getText().toString());
            map.put("phone", etPhone.getText().toString());
            map.put("address", etSpecific.getText().toString());
            map.put("isSelected", defaultAddressCheck.isChecked() ? "1" : "0");
            map.put("area", etDistrict.getText().toString());
            MallRequestUtil.getRequest(TAG, ADDRESS_CREATE_URL, map, new MallRequestUtil.MallRequestCallback() {
                @Override
                public void onSuccess(String response) {
                    Log.e("MallCart", response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject.has("result") && jsonObject.getString("result").equals("success")) {
                            String addressId = jsonObject.getString("addressId");
                            Intent intent = new Intent();
                            intent.putExtra("addressId", addressId);
                            setResult(RESULT_OK, intent);
                            finish();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFail(String error) {

                }
            });
        } else if (id == R.id.transparent_view || id == R.id.iv_back) {
            finish();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EFHttpRequest.cancelRequest(TAG);
    }
}
